<template>
  <div class="szoneButton">
    <el-button type="primary" plain size='small' style="margin-left:10px" @click="szoneDialogVisible('添加地址')" class="addsite">{{$t('添加IP')}}</el-button>
    <el-dialog :close-on-click-modal='false' v-drag :title="$t('添加')" append-to-body :v-if="dialogVisibleType" :visible.sync="dialogVisibleType" top="20vh" width="470px">
      <div class="dialogContentBoxMaxHeight60">
        <el-form :hide-required-asterisk="true" ref="form" label-position="left" style="margin:0 20px;">
          <el-form-item :label="$t('添加类型')" label-width="100px">
            <el-radio-group v-model="title" class="strategyAddrRadioGroup">
              <el-radio label="添加地址" class="addAdress">{{$t('添加地址')}}</el-radio>
              <el-radio label="添加地址组" class="addGroup">{{$t('添加地址组')}}</el-radio>
              <el-radio label="添加资产" class="addSource">{{$t('添加资产')}}</el-radio>
              <el-radio label="添加资产组" class="addSourceGroup">{{$t('添加资产组')}}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="szoneTypeDialogVisible" :class="nameType=='sip'?'innerAddSubmitSip':'innerAddSubmitDip'">{{$t('确 定')}}</el-button>
        <el-button @click="closeTypeDialog" :class="nameType=='sip'?'innerAddCloseSip':'innerAddCloseDip'">{{$t('取 消')}}</el-button>
      </div>
    </el-dialog>
    <el-dialog :close-on-click-modal='false' v-drag :title="$t(title)" append-to-body :v-if="dialogVisible" :visible.sync="dialogVisible" top="10vh" :width="titleSizeList[title]">
      <div class="dialogContentBoxMaxHeight80">
        <!-- 添加页面 -->
        <AddrGroupTools ref="child_form" v-if="title === '添加地址组' && dialogVisible" @childForm="handleChildFormGroup" />
        <AddrTools ref="child_form" v-if="title === '添加地址' && dialogVisible" @childForm="handleChildForm" />
        <AssetgroupDialog ref="child_form" v-if="title === '添加资产组' && dialogVisible" @childForm="handleChildAssetGroup" />
        <AssetobjectDialog ref="child_form" v-if="title === '添加资产' && dialogVisible" @childForm="handleChildAsset" />
      </div>
      <!-- 弹窗控件 -->
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="getChildData" class="innerAddSubmit">{{$t('确 定')}}</el-button>
        <el-button @click="closeDialog" class="innerAddClose">{{$t('取 消')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>

import { addAddrInfo } from '@/api/list/addr'
import { addAddrGroup } from '@/api/list/addr-group'
import { addAsset } from '@/api/list/assets/assetobject'
import { addAssetGroup } from '@/api/list/assets/assetGroup'
import AddrGroupTools from '@/components/addrTools/AddrGroupTools'
import AddrTools from '@/components/addrTools/AddrTools'
import AssetgroupDialog from './assetTools/AssetgroupDialog'
import AssetobjectDialog from './assetTools/AssetobjectDialog'
export default {
  name: 'AddrDialog',
  props: ['nameType'],
  components: {
    AddrGroupTools,
    AddrTools,
    AssetgroupDialog,
    AssetobjectDialog
  },
  data () {
    return {
      dialogVisible: false,
      dialogVisibleType: false,
      title: '添加地址',
      titleSizeList: {
        '添加地址': '470px',
        '添加地址组': '700px',
        '添加资产': '870px',
        '添加资产组': '600px'
      }
    }
  },
  methods: {
    szoneDialogVisible () {
      this.title = '添加地址'
      this.dialogVisibleType = true
    },
    szoneTypeDialogVisible () {
      this.dialogVisible = true
      this.dialogVisibleType = false
    },
    getChildData () {
      this.$refs.child_form.sendCommand()
    },
    handleChildFormGroup (data, name) {
      addAddrGroup(data).then(res => {
        if (res.status === 0) {
          this.$messages.success(this.$t('添加成功'))
          this.$emit('commandList', data, this.nameType, name)
          this.closeDialog()
        }
      })
    },
    handleChildForm (data, name) {
      addAddrInfo(data).then(res => {
        if (res.status === 0) {
          this.$messages.success(this.$t('添加成功'))
          this.$emit('commandList', data, this.nameType, name)
          this.closeDialog()
        }
      })
    },
    handleChildAsset (data, name) {
      addAsset(data).then(res => {
        if (res.status === 0) {
          this.$messages.success(this.$t('添加成功'))
          this.$emit('commandList', data, this.nameType, name)
          this.closeDialog()
        }
      })
    },
    handleChildAssetGroup (data, name) {
      addAssetGroup(data).then(res => {
        if (res.status === 0) {
          this.$messages.success(this.$t('添加成功'))
          this.$emit('commandList', data, this.nameType, name)
          this.closeDialog()
        }
      })
    },
    closeTypeDialog () {
      this.dialogVisibleType = false
      this.dialogVisible = false
    },
    closeDialog () {
      this.dialogVisible = false
      this.$refs.child_form.closeDialog()
    }
  }
}
</script>
<style lang="scss" scoped>
.szoneButton {
  display: inline;
}

.strategyAddrRadioGroup {
  .el-radio {
    line-height: 40px;
  }
}
</style>
